// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// V [author] rhyme_lph
// o [email]  rhymelph@gmail.com
// i [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/widgets/button/normal_border_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:finance_app/src/widgets/input/input_name_field.dart';
import 'package:finance_app/src/widgets/list_item/list_header_item.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/phone_number_input_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/switch/normal_switch.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/setting/account_settings_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_svg/svg.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "accountSettingsPage")
class AccountSettingsPage extends StatefulWidget {
  @override
  _AccountSettingsPageState createState() => _AccountSettingsPageState();
}

class _AccountSettingsPageState extends State<AccountSettingsPage>
    with PagePresenter<AccountSettingsPage, AccountSettingsProvider> {
  @override
  PreferredSizeWidget get appBar =>
      getDefaultAppBar(title: S.current.account_settings, actions: [
        NormalSaveTextButton(
          onTap: provider.onSave,
        )
      ]);

  @override
  Widget get buildWidget => FormEditStatusInheritedWidget(
        isEdit: true,
        child: ColoredBox(
          color: Colors.white,
          child: ListView(
            children: [
              _buildPersonInfoItem(),
              AppSpaces.h20,
              ListHeaderItem(
                title: S.current.personal_details,
              ),
              FromInputField(
                controller: provider.displayNameController,
                focusNode: provider.displayNameFocusNode,
                tip: S.current.display_name,
                border: null,
              ),
              ListHeaderItem(
                title: S.current.login_credentials,
              ),
              rootListBuilder<String>(
                selector: (_, __) => Config.userProfile?.email,
                builder: (BuildContext context, String value, Widget child) {
                  return ValueSelectForm(
                    title: S.current.email,
                    hasArrow: false,
                    onTap: provider.onUpdateEmail,
                    value: value,
                  );
                },
              ),
              rootListBuilder<Tuple2<String, String>>(
                  builder: (BuildContext context, Tuple2<String, String> value,
                      Widget child) {
                    provider.phoneNumberController.text = value.item2;
                    return PressedOpacityButton(
                      onPressed: provider.onUpdatePhoneNumber,
                      child: IgnorePointer(
                        child: PhoneNumberInputForm(
                          controller: provider.phoneNumberController,
                          focusNode: provider.phoneNumberFocusNode,
                          defaultCountryCode: value.item1,
                          countryCodeCallback: (data) =>
                              provider.onUpdateCountryCode(data),
                        ),
                      ),
                    );
                  },
                  selector: (_, __) => Tuple2<String, String>(
                      Config.userProfile?.countryCode,
                      Config.userProfile?.phoneNumber)),
              ValueSelectForm(
                title: S.current.password,
                value: '********',
                onTap: provider.onChangePassword,
              ),
              rootListBuilder<bool>(
                selector: (_, __) => Config.biometricsEnable == true,
                builder: (BuildContext context, bool value, Widget child) =>
                    NormalFormWrapper(
                  child: PressedOpacityButton(
                    onPressed: provider.enableBiometrics,
                    child: NormalSwitch(
                      title: S.current.biometric_login,
                      isSelect: value,
                    ),
                  ),
                ),
              ),
              ListHeaderItem(
                title: S.current.preferences,
              ),
              rootListBuilder<String>(
                selector: (_, __) => Config.languageCode,
                builder: (BuildContext context, String value, Widget child) =>
                    ValueSelectForm(
                  title: S.current.language,
                  onTap: provider.onChangeLanguage,
                  value: LanguageUtils.getLanguageName(value),
                ),
              ),
              AppSpaces.h40,
              Center(
                child: NormalBorderButton(
                  title: S.current.logout,
                  onTap: provider.onLogout,
                  isFit: true,
                ),
              ),
              AppSpaces.h90,
            ],
          ),
        ),
      );

  Widget _buildPersonInfoItem() {
    return NormalFormWrapper(
      leftPadding: getDp(25),
      topPadding: getDp(19),
      bottomPadding: getDp(0),
      rightPadding: getDp(20),
      child: Row(
        children: [
          listBuilder<String>(
            selector: (_, __) => __.networkPath,
            builder: (BuildContext context, String avatar, Widget child) =>
                listBuilder<String>(
              selector: (_, __) => __.imagePath,
              builder: (BuildContext context, String value, Widget child) =>
                  PressedOpacityButton(
                onPressed: provider.onUpdateLoadAvatar,
                child: CircleAvatar(
                  backgroundColor: aceWhiteGreyColor,
                  backgroundImage: Config.networkToLocalProvider(avatar, value),
                  radius: getDp(34.5),
                  child: avatar == null && value == null
                      ? SvgPicture.asset(
                          A.assetsImagesStylePhoto_camera_interface_symbol_for_button_2,
                          width: getDp(28),
                          height: getDp(28),
                        )
                      : null,
                ),
              ),
            ),
          ),
          SizedBox(
            width: getDp(15),
          ),
          Expanded(
            child: InputBoxShadowField(
              controller: provider.fullNameController,
              focusNode: provider.fullNameFocusNode,
              tip: S.of(context).full_name,
            ),
          )
          // ValueSelectForm()
        ],
      ),
    );
  }
}
